home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / VOL_400 / 466_01 / SRC / PARSE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-20  |  6.1 KB  |  302 lines

  1. /*
  2.  * parse.c
  3.  */
  4.  
  5. #include <afx.h>
  6. #include <afxtempl.h>
  7. #include "parse.h"
  8.  
  9.  
  10. void ZeroMem(void *p, size_t nLen)
  11. {
  12.     memset(p, 0, nLen);
  13. }
  14.  
  15. const char *MatchParen(
  16.     const char *sz, 
  17.     char chEndMatch, 
  18.     int nSkipEmbedded)
  19. {
  20.     ++sz;
  21.     while(*sz && *sz != chEndMatch)
  22.     {
  23.         if(nSkipEmbedded)
  24.         {
  25.             switch(*sz)
  26.             {
  27.             case chOpenParen:
  28.                 sz = MatchParen(sz, chCloseParen, TRUE);
  29.                 if(*sz)
  30.                     ++sz;
  31.                 break;
  32.  
  33.             case chOpenBracket:
  34.                 sz = MatchParen(sz, chCloseBracket, TRUE);
  35.                 if(*sz)
  36.                     ++sz;
  37.                 break;
  38.  
  39.             case chOpenAngle:
  40.                 sz = MatchParen(sz, chCloseAngle, TRUE);
  41.                 if(*sz)
  42.                     ++sz;
  43.                 break;
  44.             
  45.             default:
  46.                 ++sz;
  47.                 break;
  48.             }
  49.         }
  50.         else
  51.         {
  52.             switch(*sz)
  53.             {
  54.             case chOpenParen:
  55.             case chOpenBracket:
  56.             case chOpenAngle:
  57.                 return sz;
  58.             
  59.             default:
  60.                 ++sz;
  61.                 break;
  62.             }
  63.         }
  64.     }
  65.  
  66.     return sz;
  67. }
  68.  
  69.  
  70. /*
  71. @func Searches backward through a line and retreats past white space
  72. characters.
  73.  
  74. @rdesc Position of first non-white character preceding starting point,
  75. or <p szStart>, whichever comes first..
  76.  
  77. @ex The following example returns the space following letter "w": |
  78.  
  79. Foobaria pentaria how                          grubstake
  80.                     ^                          ^
  81.           result ---+                          +--starting point
  82. */
  83.  
  84. const char *TrimWhite(
  85.     const char *sz,       //@parm Starting point for backwards search
  86.     const char *szBound)  //@parm First legal position to search (backwards 
  87.                             // boundary)
  88. {
  89.     ASSERT(sz);
  90.     ASSERT(szBound);
  91.     ASSERT(sz > szBound);
  92.  
  93.     for(sz--; sz != szBound && isspace(*sz); sz--);
  94.  
  95.     return sz;
  96. }
  97.  
  98.  
  99.  
  100. /*-----------------------------------------------------------------------
  101. @doc EXTERNAL AUTODUCK
  102.         
  103. @func char * | EatWhite | Advances past whitespace characters and
  104. returns first non-white character.
  105.             
  106. @parm char * | sz | Specifies beginning of white text.
  107.     
  108. @rdesc Returns a pointer to the first non-white character following
  109. <p sz>.
  110.  
  111. */
  112. const char *EatWhite(const char *sz)
  113. {
  114.     ASSERT(sz);
  115.     
  116.     while(isspace(*sz))
  117.         sz++;
  118.  
  119.     return sz;
  120. }
  121.  
  122.  
  123. /*-----------------------------------------------------------------------
  124. @doc EXTERNAL AUTODUCK
  125.         
  126. @func char * | EatWhiteNl | Advances past whitespace characters and
  127. returns first non-white character. Keeps track of current line
  128. number.
  129.             
  130. @parm char * | sz | Specifies beginning of white text.
  131.  
  132. @parm int * | pnLine | Specifies a pointer to the current
  133. line count. 
  134.     
  135. @rdesc Returns a pointer to the first non-white character following
  136. <p sz>.
  137.  
  138. */
  139. const char *EatWhiteNl(const char *sz, int *pnLineNum)
  140. {
  141.     // Skip leading whitespace
  142.  
  143.     while (isspace(*sz))
  144.     {
  145.         if (*sz++ == '\n')
  146.         {
  147.             ++(*pnLineNum);
  148.         }
  149.     }
  150.     return sz;
  151. }
  152.  
  153.  
  154.  
  155. /*-----------------------------------------------------------------------
  156. @doc EXTERNAL AUTODUCK
  157.         
  158. @func char * | SeekChar | Advances past whitespace characters to
  159. the specified character.
  160.             
  161. @parm char * | sz | Specifies beginning of text.
  162.     
  163. @rdesc Returns a pointer to the character or NULL if the first non-white
  164. character is not the specified character.
  165.  
  166. */
  167. const char *SeekChar(const char *sz, char ch)
  168. {
  169.     sz = EatWhite(sz);
  170.     if(*sz != ch)
  171.         return NULL;
  172.     return sz;
  173. }
  174.  
  175.  
  176. /*-----------------------------------------------------------------------
  177. @doc EXTERNAL AUTODUCK
  178.         
  179. @func char * | SeekNumber | Advances past whitespace characters to
  180. the first numeric character.
  181.             
  182. @parm char * | sz | Specifies beginning of text.
  183.     
  184. @rdesc Returns a pointer to the number or NULL if the first non-white
  185. character is not a number.
  186.  
  187. */
  188. const char *SeekNumber(const char *sz)
  189. {
  190.     sz = EatWhite(sz);
  191.     if(!isdigit(*sz))
  192.         return NULL;
  193.     return sz;
  194. }
  195.  
  196.  
  197. /*-----------------------------------------------------------------------
  198. @doc EXTERNAL AUTODUCK
  199.         
  200. @func char * | SeekLetter | Advances past whitespace characters to
  201. the first alphabetic character.
  202.             
  203. @parm char * | sz | Specifies beginning of text.
  204.     
  205. @rdesc Returns a pointer to the letter or NULL if the first non-white
  206. character is not a letter.
  207.  
  208. */
  209. const char *SeekLetter(const char *sz)
  210. {
  211.     sz = EatWhite(sz);
  212.     if(!isalpha(*sz))
  213.         return NULL;
  214.     return sz;
  215. }
  216.  
  217.  
  218. /*-----------------------------------------------------------------------
  219. @doc EXTERNAL AUTODUCK
  220.         
  221. @func char * | MakeNumber | Constructs an integer value from text.
  222.             
  223. @parm char * | sz | Specifies beginning of integer text.
  224.     
  225. @parm int * | pi | Specifies where to store the integer value.
  226.     
  227. @rdesc Returns a pointer to the first non-number character following
  228. <p sz>.
  229.  
  230. */
  231. const char *MakeNumber(const char *sz, int &i)
  232. {
  233.     int bNeg;
  234.  
  235.     ASSERT(sz);
  236.     
  237.     if(*sz == chMinus)
  238.     {
  239.         sz++;
  240.         if(!isdigit(*sz))
  241.             return NULL;
  242.             
  243.         bNeg = TRUE;
  244.     }
  245.     else
  246.     {
  247.         bNeg = FALSE;
  248.     }
  249.  
  250.     for(i = 0; isdigit(*sz); sz++)
  251.         i = i*10 + (*sz-'0');
  252.  
  253.     if(bNeg)
  254.         i *= -1;
  255.  
  256.     return sz;
  257. }
  258.  
  259.  
  260.  
  261. const char *SeekEnd(const char *szSrc, const char *szStopChars, int nMax)
  262. {
  263.     int i;
  264.  
  265.     ASSERT(szSrc);
  266.     ASSERT(szStopChars);
  267.     ASSERT(nMax > 0);
  268.  
  269.     // Seek a stop char.
  270.     for(i = 0; i < nMax && *szSrc && !strchr(szStopChars, *szSrc); 
  271.         szSrc++, i++);
  272.  
  273.     // Detect overflow situation
  274.     if(nMax == i && (szSrc[i] && !strchr(szStopChars, *szSrc)))
  275.         return NULL;
  276.  
  277.     return szSrc;
  278. }
  279.  
  280.  
  281.  
  282. const char *SeekEnd(const char *szSrc, char chStopChar, int nMax)
  283. {
  284.     int i;
  285.  
  286.     ASSERT(szSrc);
  287.     ASSERT(nMax > 0);
  288.  
  289.     // Seek a stop char.
  290.     for(i = 0; i < nMax && *szSrc && *szSrc != chStopChar; szSrc++, i++);
  291.  
  292.     // Detect overflow situation
  293.     if(nMax == i && (szSrc[i] && *szSrc != chStopChar))
  294.         return NULL;
  295.  
  296.     return szSrc;
  297. }
  298.  
  299.  
  300.  
  301.  
  302.